package newsSystem.Test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainFrameTest extends JFrame {
    private JMenuBar menuBar;
    private JMenu menu1;
    private JMenu menu2;
    private JMenu menu3;
    private JPanel contentPanel;

    public MainFrameTest() {
        setTitle("报刊订阅管理系统");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800, 600);
        setLocationRelativeTo(null); // 居中显示

        // 创建菜单栏
        menuBar = new JMenuBar();

        // 创建一级菜单
        menu1 = new JMenu("管理员首页");
        menu2 = new JMenu("用户信息管理");
        menu3 = new JMenu("部门信息管理");

        // 将一级菜单添加到菜单栏中
        menuBar.add(menu1);
        menuBar.add(menu2);
        menuBar.add(menu3);

        // 设置菜单栏
        setJMenuBar(menuBar);

        // 创建内容面板
        contentPanel = new JPanel(new CardLayout()); // 使用CardLayout来切换页面
        JLabel page1 = new JLabel("This is Page 1");
        JLabel page2 = new JLabel("This is Page 2");
        JLabel page3 = new JLabel("This is Page 3");
        contentPanel.add(page1, "Page 1");
        contentPanel.add(page2, "Page 2");
        contentPanel.add(page3, "Page 3");

        // 添加菜单项的点击事件
        JMenuItem menuItem1 = new JMenuItem("Page 1");
        JMenuItem menuItem2 = new JMenuItem("Page 2");
        JMenuItem menuItem3 = new JMenuItem("Page 3");

        menu1.add(menuItem1);
        menu2.add(menuItem2);
        menu3.add(menuItem3);

        menuItem1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout cardLayout = (CardLayout) contentPanel.getLayout();
                cardLayout.show(contentPanel, "Page 1");
            }
        });

        menuItem2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout cardLayout = (CardLayout) contentPanel.getLayout();
                cardLayout.show(contentPanel, "Page 2");
            }
        });

        menuItem3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout cardLayout = (CardLayout) contentPanel.getLayout();
                cardLayout.show(contentPanel, "Page 3");
            }
        });

        // 将内容面板添加到主窗口中
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(contentPanel, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MainFrameTest().setVisible(true);
            }
        });
    }
}
